package com.example.xiaoyuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xiaoyuan.mapper.TeacherMapper;
import com.example.xiaoyuan.pojo.Admin;
import com.example.xiaoyuan.pojo.LoginForm;
import com.example.xiaoyuan.pojo.Teacher;
import com.example.xiaoyuan.service.TeacherService;
import com.example.xiaoyuan.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Teacher login(LoginForm loginForm) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        Teacher teacher = baseMapper.selectOne(queryWrapper);

        return teacher;
    }

    @Override
    public Teacher getTeacherById(Long userId) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);

        Teacher teacher = baseMapper.selectOne(queryWrapper);

        return teacher;
    }

    @Override
    public IPage<Teacher> getTeacherByOpr(Page<Teacher> teacherPage, Teacher teacher) {

        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(teacher.getName() !=null, Teacher::getName, teacher.getName());

        queryWrapper.eq(teacher.getClazzName() !=null, Teacher::getClazzName, teacher.getClazzName());

        queryWrapper.orderByAsc(Teacher::getId);

        Page<Teacher> page = baseMapper.selectPage(teacherPage, queryWrapper);
        return page;
    }
}
